import request from "../utils/request"

/**
 * 
 * @param {Object} data 
 * @param {String} key 
 * @param {String} target 
 * @returns 
 */
function Percentage(data, key) {

  return data;
}

function FetchStatisticsOrder(component)
{
  request.post("/order/statisticsOrder").then(res => {
    component.totalOrdersToday = res.data.totalOrdersToday;
    component.totalSalesToday = res.data.totalSalesToday;
    component.totalSalesYesterday = res.data.totalSalesToday;
    component.orderToBePaid= res.data.orderToBePaid;
    component.orderCompleted= res.data.orderCompleted;
    component.orderCancelled = res.data.orderCancelled;

    component.numberOfOrdersThisWeek = res.data.numberOfOrdersThisWeek;
    component.salesThisWeek= res.data.salesThisWeek;
    component.salesThisMonth= res.data.salesThisMonth;

    component.ordersOfThisMonth = res.data.ordersOfThisMonth;
    component.ordersOfLastMonth = res.data.ordersOfLastMonth;
    component.numberOfOrdersLastWeek= res.data.numberOfOrdersLastWeek;
    component.salesLasWeek= res.data.salesLasWeek;
    component.salesLasMonth = res.data.salesLasMonth;

    
    if (0 == component.ordersOfLastMonth) {
      component.yearOnYearLastMonthByOrderCount = "0";
    } else {
      
      component.yearOnYearLastMonthByOrderCount = 
        ((component.ordersOfThisMonth - component.ordersOfLastMonth)
         / component.ordersOfLastMonth)* 100
        
      component.yearOnYearLastMonthByOrderCount = component.yearOnYearLastMonthByOrderCount.toFixed(2)

    }

    if (0 == component.numberOfOrdersLastWeek) {
      component.yearOnYearLastMonthByWeekCount ="0";
    } else {
      component.yearOnYearLastMonthByWeekCount = ((component.numberOfOrdersThisWeek 
        - component.numberOfOrdersLastWeek) / component.numberOfOrdersLastWeek)* 100;

        component.yearOnYearLastMonthByWeekCount = component.yearOnYearLastMonthByWeekCount.toFixed(2)

    }

    if (0 == component.salesLasWeek) {
      component.yearOnYearLastMonthByWeekSales = "0";
    } else {
      component.yearOnYearLastMonthByWeekSales = ((component.salesThisWeek 
        - component.salesLasWeek) / component.salesLasWeek)* 100
        component.yearOnYearLastMonthByWeekSales
          = component.yearOnYearLastMonthByWeekSales.toFixed(2)
    }

    if (0 == component.salesLasMonth) {
      component.yearOnYearLastMonthByMonthSales = "0";
    } else {
      component.yearOnYearLastMonthByMonthSales = ((component.salesThisMonth 
        - component.salesLasMonth) / component.salesLasMonth)* 100
        component.yearOnYearLastMonthByMonthSales
        = component.yearOnYearLastMonthByMonthSales.toFixed(2)
    }

  });
}

function FetchStatisticsMarket(component)
{
  request.post("/market/getStatisticsMarket").then(res => {
    component.alreayShelves = res.data.alreayShelves;
    component.offShelf = res.data.offShelf;
    component.allProducts = res.data.allProducts;
  });
}

function FetchStatisticsOrderBetween(data, component) {

  request.post("/order/quantityWithinTheTime", data).then(res => {
      console.log(res.data);
      component.chartData.columns = ['startTime', 'count','price'];
      res.data.forEach((e,i) => {
        component.$set(component.chartData.rows,
        i, e
        );
        component.chartData.rows[i] = e;
      })
  
      console.log()
      component.dataEmpty = false;
      component.loading = false

  

  });

}

function FetchStatisticsUser(component)
{
  request.post("/user/getStatisticsUserVO").then(res => {
    console.log("res", res);
    component.newlyAddedByToday = res.data.newlyAddedByToday;
    component.newlyAddedByYesterday = res.data.newlyAddedByYesterday;
    component.newThisMonth = res.data.newThisMonth;
    component.memberTotalNumber = res.data.memberTotalNumber;
  });
}


export {
  FetchStatisticsOrder,
  FetchStatisticsUser,
  FetchStatisticsOrderBetween,
  FetchStatisticsMarket
}
